﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Ch15MidBasic
{
    public partial class frmChild : Form
    {   
        public frmChild(frmContainer parent,int counter)
        {
            InitializeComponent();
            this.toolStripComboBoxFonts.SelectedIndex = 0;
            this.MdiParent = parent;
            
             this.Text = "Untitled " + counter.ToString();
                    
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.richTextBoxContent.Text = "";
        }

        public void OpenFile(string filename)
        {
            try
            {
                this.richTextBoxContent.Text = File.ReadAllText(filename);
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message, "Editor: ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        public void SaveFile(string filename)
        {
            try
            {
                File.WriteAllText(filename, richTextBoxContent.Text);
                this.Text = filename;
                
            }
            catch(IOException ex)
            {
                MessageBox.Show(ex.Message, "Editor: ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlgSaveFile = new SaveFileDialog();
            dlgSaveFile.Filter = "WordPad(*.rtf) |*.rtf|Text file (*.txt)|*.txt|All file(*.*)|*.*";
            dlgSaveFile.Title = "Save File";
            if (dlgSaveFile.ShowDialog() == DialogResult.OK)
            {
                SaveFile(dlgSaveFile.FileName);
                
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.Text.IndexOf("\\") == -1)
            {
                saveAsToolStripMenuItem_Click(sender, e);
            }
            else
            {
                SaveFile(this.Text);
            }

        }

        private void frmChild_Activated(object sender, EventArgs e)
        {
            this.MdiParent.Text = "MDI:" + this.Text;
        }

        private void frmChild_TextChanged(object sender, EventArgs e)
        {
            this.MdiParent.Text = "MDI:"+this.Text;
        }


    }
}
